package com.rocket.bedroom.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rocket.bedroom.dao.RoomDao;
import com.rocket.bedroom.dao.custom.RoomCustomMapper;
import com.rocket.bedroom.exceptions.RepeatNameException;
import com.rocket.bedroom.exceptions.RoomException;
import com.rocket.bedroom.pojo.BerthEntity;
import com.rocket.bedroom.pojo.FloorEntity;
import com.rocket.bedroom.pojo.custom.RoomCustomEntity;
import com.rocket.bedroom.pojo.RoomEntity;
import com.rocket.bedroom.pojo.param.RoomParam;
import com.rocket.bedroom.service.IBerthService;
import com.rocket.bedroom.service.IFloorService;
import com.rocket.bedroom.service.IRoomService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 12:41 2020/5/4
 * @ModifyBy:
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomDao, RoomEntity> implements IRoomService {

    @Resource
    private IFloorService floorService;

    @Resource
    private IBerthService berthService;

    @Resource
    private RoomCustomMapper roomCustomMapper;


    /**
     * 根据楼层和房间数量，批量开辟房间及房间床位
     *
     * @param floorEntity 楼层对象
     * @param roomNumber  房间数量
     * @return
     */
    @Override
    public boolean addRoomAndBethByFloor(FloorEntity floorEntity, Integer roomNumber) {
        Assert.notNull(floorEntity, "addRoomAndBethByFloor -> 楼层对象 IS NULL");
        Assert.notNull(floorEntity.getBlockId(), "addRoomAndBethByFloor -> 楼层 blockId IS NULL");
        Assert.isTrue(0 != roomNumber, "addRoomAndBethByFloor -> 楼层 roomNumber == 0");
        if (0 != floorService.hasFloorName(floorEntity.getFloorName(), floorEntity.getBlockId())) {
            throw new RepeatNameException("楼层存在重复", 500);
        }
        boolean saveFloor = floorService.save(floorEntity);
        Assert.notNull(floorEntity.getFloorId(), "addRoomAndBethByFloor -> 楼层 floorId IS NULL");
        int rmCount = 0;
        for (int i = 1; i <= roomNumber; i++) {
            RoomEntity addRoomObj = new RoomEntity();
            addRoomObj.setFloorId(floorEntity.getFloorId());
            addRoomObj.setBlockId(floorEntity.getBlockId());
            String floorName = floorEntity.getFloorName();
            String roomPrefix = floorName.toUpperCase().substring(0, floorName.lastIndexOf("F"));

            if (i < 10) {
                addRoomObj.setRoomName(roomPrefix + "0" + i);
            } else {
                addRoomObj.setRoomName(roomPrefix + i);
            }

            boolean saveRoomRS = super.save(addRoomObj);
            if (saveRoomRS) {//开辟房间中的床位
                this.addBerth(addRoomObj, 6);
                rmCount++;
            } else {
                throw new RoomException("添加" + addRoomObj.getRoomName() + "房间时出现异常", 500);
            }
        }
        //判断添加房间数量是否正确，如果不正确触发异常进行事务回滚
        if (roomNumber == rmCount) {
            return true;
        } else {
            throw new RoomException("添加房间时出现异常", 500);
        }
    }


    /**
     * 查询 RoomCustom
     *
     * @param roomParam
     * @return
     */
    public List<RoomCustomEntity> selectRoomCustom(RoomParam roomParam) {
        List<RoomCustomEntity> roomCustomEntities = roomCustomMapper.selectRoomCustom(roomParam);
        return roomCustomEntities;
    }

    /**
     * 添加单个房间
     *
     * @param roomEntity
     * @return
     */
    @Override
    public boolean addRoomAndBerth(RoomEntity roomEntity, Integer berthNumber) {
        Assert.notNull(roomEntity, "ADD Room IS NULL");
        Assert.notNull(roomEntity.getRoomName(), "ADD Room RoomName IS NULL");
        boolean saveRoomRS = super.save(roomEntity);
        if (saveRoomRS) {//开辟房间中的床位
            return this.addBerth(roomEntity, berthNumber);
        } else {
            throw new RoomException("添加" + roomEntity.getRoomName() + "房间时出现异常", 500);
        }
    }


    /**
     * 添加房间时间，开辟新的床位
     *
     * @param roomEntity  添加房间对象》添加床位时需要房间信息
     * @param berthNumber 开辟床位数量
     * @return
     */
    private boolean addBerth(RoomEntity roomEntity, Integer berthNumber) {
        List<BerthEntity> berthList = new ArrayList<>();
        String[] berthIndexArr = {"A", "B", "C", "D", "E", "F"};
        for (int j = 0; j < berthNumber; j++) {
            BerthEntity berthEntity = new BerthEntity();
            berthEntity.setRoomId(roomEntity.getRoomId());
            //两个中文状态的 减号“—”
            berthEntity.setBerthName(roomEntity.getRoomName() + "——" + berthIndexArr[j]);
            berthList.add(berthEntity);
        }
        return berthService.saveBatch(berthList);
    }
}
